<?php
// $Id$

/**
 * @file
 * TODO
 */

function _portals_render($portalid, $args = array()) {
  portals_include('content');
  $config_file = _portals_get_cnf_from_pid($portalid);
  $output = array();
  if ($config_file != FALSE) {
    foreach ($config_file as $config_item) { 
      $new_item['html'] = _portals_display_item($config_item->type, $config_item->args);
      if (isset($config_item->layout)) {
        $new_item['layout'] = $config_item->layout;
      }
      else {
        $new_item['layout']['row'] = 0;
        $new_item['layout']['col'] = 0;
        $new_item['layout']['pri'] = 0;
      }
     
      $output[] = $new_item;
    }
  }
  return theme('portals_view_portals', $output);
}

function _portals_display_item($type, $args) {
  if ($type == 'block') {
    return _portals_fetch_block($args[0], $args[1]);
  }
  elseif ($type == 'page') {
    return _portals_fetch_page($args[0]);
  }
  elseif ($type == 'frame') {
    // Put Embedded Frame View Here
    return 'Frames Unsupported';
  }
  return 'Unknown Item type';
}

function _portals_fetch_block($module_id, $blockid) {
  $output_data = '';
  if (module_hook($module_id, 'block')) {
    $module_info = module_invoke($module_id, 'block', 'view', $blockid);
    $output_data .= (isset($module_info['content']) ? $module_info['content'] : 'Module '. $module_id .' block '. $blockid .'is blank.');
  }
  else {
    $output_data .= "Unknown Module '". $module_id ."', or module does not have any blocks to display.";
  }
  return $output_data;
}

function _portals_fetch_page($url) {
  $output_data = '';
  $page_title = drupal_get_title();
  $trail_info = menu_get_active_trail();
  $output_data .= menu_execute_active_handler($url);
  menu_set_active_trail($trail_info);
  drupal_set_title($page_title);
  return $output_data;
}